package com.example.justbuy.action.buyer;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.nio.file.Paths;
import java.util.UUID;

@Controller
public class shangchuan {

    @RequestMapping("/sc.htm")
    public String regist(){
        return "上传图片.html";
    }
    @RequestMapping("/shangchuan.htm")
    public String save(HttpServletRequest request, MultipartFile[] items_pic, String uname, Model md) throws  Exception {
        String pic_path = request.getSession().getServletContext().getRealPath("/");
        for (int i=0;i<items_pic.length;i++){
            String originalFilename = items_pic[i].getOriginalFilename();
            if(items_pic!=null && originalFilename!=null && originalFilename.length()>0){
                //存储图片的物理路径
                //新的图片名称
                String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                md.addAttribute("pic_path",newFileName);
                //新图片
                File newFile = new File(pic_path + newFileName);
                items_pic[i].transferTo(newFile);
            }
        }
        return "上传图片.html";
    }
}
